﻿using AiXiu.Common;
using AiXiu.IService;
using AiXiu.Model;
using AiXiu.Service;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace AiXiu.WebSite.Controllers
{
    [Authorize]
    public class VideoController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Publish()
        {
            return View();
        }

        [HttpGet]
        public ActionResult VideoUpload(string filename, string headline,string location)
        {
            // 参数
            filename = HttpUtility.UrlDecode(filename);
            headline = HttpUtility.UrlDecode(headline);
            if (string.IsNullOrWhiteSpace(headline))
            {
                headline = "短视频";
            }
            location = HttpUtility.UrlDecode(location);

            //调用方法获取上传凭证
            VodHelper vodHelper = new VodHelper();
            CreateUploadVideoResult result = vodHelper.CreateUploadVideo(headline, filename);

            //读取用户Id
            TBUsers user = IdentityManager.ReadUser();

            //视频信息保存到数据库
            IVideoService videoService = new VideoService();
            TBVideos video = new TBVideos();
            video.VideoId = result.VideoId;

            int userId = user.Id;
            video.UserId = userId;
            video.Headline = headline;
            video.Location = location;
            videoService.AddVideo(video);

            return Json(result,JsonRequestBehavior.AllowGet);
        }

        public ActionResult MyVideos()
        {
            TBUsers user = IdentityManager.ReadUser();
            IVideoService videoService = new VideoService();
            List<TBVideos> list = videoService.GetVideosByUserId(user.Id);
            return View(list);
        }

        public ActionResult VideoDelete(string videoId)
        {
            IVideoService videoService = new VideoService();
            videoService.DeleteVideo(videoId);
            return null;
        }

        /// <summary>
        /// 视频播放
        /// </summary>
        /// <param name="videoId"></param>
        /// <returns></returns>
        public ActionResult Play(string videoId)
        {
            IVideoService videoService = new VideoService();
            TBVideos video = videoService.GetVideoByVideoId(videoId);

            //获取视频播放地址
            VodHelper vodHelper = new VodHelper();
            GetPlayInfoResult playInfo = vodHelper.GetPlayInfo(video.VideoId);
            ViewBag.playInfo = playInfo;

            //点赞
            ILikeService likeService = new LikeService();
            TBUsers user = IdentityManager.ReadUser();
            ViewBag.isLike = likeService.IsLike(videoId, user.Id);
            ViewBag.likeCount = likeService.Count(videoId);

            //评论
            IDiscussService discussService = new DiscussService();
            ViewBag.discussCount= discussService.Count(videoId);
            return View(video);
        }

        //点赞 or 取消点赞
        public ActionResult VideoLike(string videoId,int userId)
        {
            ILikeService likeService = new LikeService();
            if (likeService.IsLike(videoId, userId))
            {
                likeService.RemoveLike(videoId, userId);
            }
            else
            {
                likeService.AddLike(videoId, userId);
            }
            return null;
        }

        /// <summary>
        /// 视频评论页面
        /// </summary>
        /// <param name="videoId">视频id</param>
        /// <returns></returns>
        public ActionResult Discuss(string videoId)
        {
            ViewBag.videoId = videoId;
            TBUsers user = IdentityManager.ReadUser();
            ViewBag.user = user;
            return View();
        }

        /// <summary>
        /// 获取评论列表信息
        /// </summary>
        /// <param name="videoId"></param>
        /// <param name="pageNumber"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public ActionResult DiscussList(string videoId, int pageNumber, int pageSize)
        {
            IDiscussService discussService = new DiscussService();
            List<Model.Discuss> discusses = discussService.QueryDiscusses(videoId, pageNumber, pageSize);
            // 返回结果,把Unix时间戳转换为时间格式
            string discussesJson = JsonConvert.SerializeObject(discusses, new UnixDateTimeConverter());
            return Content(discussesJson);
        }

        [HttpPost]
        public ActionResult AddDiscuss(string videoId, string NickName, string Avatar, string Content, string ReplayTo, long AddTime)
        {
            //时间戳转为时间
            DateTime addTimeValue = TimeHelper.GetTimeByUnix(AddTime);
            IDiscussService discussService = new DiscussService();
            Discuss discuss = new Discuss()
            {
                NickName = NickName,
                Avatar = Avatar,
                Content = Content,
                ReplayTo = ReplayTo,
                AddTime = addTimeValue
            };
            discussService.Add(videoId, discuss);
            return null;
        }

        public ActionResult UserVideos(int userId)
        {
            IUserService userService = new UserService();
            TBUsers user = userService.FindByUserId(userId);
            //判断是否为本人
            bool isSelf = false;
            int selfId = IdentityManager.ReadUser().Id;
            if (user.Id == selfId)
            {
                isSelf = true;
            }
            ViewBag.isSelf = isSelf;
            //判断是否为好友
            IFriendService friendService = new FriendService();
            ViewBag.isFriend = friendService.IsFriend(selfId, userId);
            return View(user);
        }
    }
}